﻿#ifndef __SYNTHE_H
#define __SYNTHE_H
#include "ESEAudio.h"

#define N_PARTIELS 40
#define N_REGIMES 129
#define N_COEFPEDALE 128
#define N_COEFVITESSE 256
#define CADR_PED 1
#define CADR_REG 	6
#define NBF         22 
#define NBE         10 
#define NBUFF       (64u)  
#define LBUFFER     (6)
#define DIV_A		(1.0f/NBUFF)
#define TABSIZE     1024 

#define ESE_WAVS				(12U)
#define ESE_PTS					(64U)
#define SAF_EQ_LEN				(244U)
#define ESE_VOLUME		   (2u)
#define ESE_FILES		   (3u)
#define MAX_ACC				(8.0f)

typedef struct
{
	unsigned char titreVersion[8];
	unsigned short fe;
	unsigned char niveau;
	unsigned char nPartiels;
	unsigned char mode;
	unsigned char nCoefvitesse;
	unsigned char nRegimes;
	unsigned char nCoefpedale;
}__attribute__((packed)) SyntheHeader2;

typedef struct
{
	unsigned short amplPartiel[N_PARTIELS];     
} Ampl;


typedef struct
{
	unsigned int phase[N_PARTIELS];   
	unsigned int freq[N_PARTIELS];    
	Ampl regime[N_REGIMES];           
	unsigned short coefPedale[N_COEFPEDALE];   
	unsigned short coefVitesse[N_COEFVITESSE]; 
	unsigned short coefPedale2[N_COEFPEDALE];  
	unsigned short coefVitesse2[N_COEFVITESSE];
	unsigned char assoLois[N_PARTIELS];        
	short offsetRpm[N_PARTIELS];       
	unsigned short coefG[N_PARTIELS];  
	unsigned short coefD[N_PARTIELS];  
} __attribute__((packed)) SyntheParam3;


typedef struct
{
	unsigned int phi;
	unsigned int dphi;
	int ddphi;                  
	unsigned int ampl;          
	int dampl;                  
	unsigned int ampG;          
	int dampG;                  
} __attribute__((packed)) Oscillateur;                           


typedef struct
{
	Oscillateur osc[N_PARTIELS];
	SyntheParam3 param;
	SyntheHeader2 header;
	unsigned char nom[20];
	unsigned int nPartiel;
	unsigned int nBuffer ;
	unsigned int lBuffer;
	unsigned short regime;
	unsigned short pedale;
	double vitesse;
	float acc;
	float volume;
} __attribute__((packed)) Synthe;


typedef struct{
	uint32_t  mute[ESE_WAVS];
	float gain[ESE_WAVS];
	uint32_t pitchDep[ESE_WAVS];
	uint32_t gain0Dep[ESE_WAVS];
	uint32_t gain1Dep[ESE_WAVS];
	float pitchX[ESE_WAVS][ESE_PTS];
	float pitchY[ESE_WAVS][ESE_PTS];
	float gain0X[ESE_WAVS][ESE_PTS];
	float gain0Y[ESE_WAVS][ESE_PTS];
	float gain1X[ESE_WAVS][ESE_PTS];
	float gain1Y[ESE_WAVS][ESE_PTS];
}SAF_PARA;

typedef struct{
	uint8_t ese_id;
	Synthe synthe;
	float ese_box_vol[ESE_FILES];
	uint32_t ese_wav_len[ESE_WAVS];
	uint32_t ese_wav_addr[ESE_WAVS];
	SAF_PARA saf_para;
	uint32_t can_pedale;
	uint32_t can_regime;
	float can_speed;
	float can_acceleration;
	uint8_t driving_state;
	uint8_t can_gear;
	int16_t can_brake;
}APP_PARA;

void syntheInit(Synthe *synthe);
void syntheAudio(APP_PARA *pApp, int * bufout);

#endif


